package com.blacktry.service.impl;

import com.blacktry.entity.model.Admin;
import com.blacktry.entity.model.AnswerNumValue;
import com.blacktry.entity.model.ApiResult;
import com.blacktry.entity.easypoidata.DeriveExcel;
import com.blacktry.service.*;
import com.blacktry.utils.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Service
public class AutoExamServiceImpl implements AutoExamService {
    @Autowired
    private AliOssService aliOssService;

    @Autowired
    private OCRService ocrService;

    @Autowired
    private AutoInfoService autoInfoService;

    @Autowired
    private EasyPoiService easyPoiService;

    @Autowired
    private DeriveExcelService deriveExcelService;

//    @Override
//    public ApiResult<String> exam(MultipartFile file,String answer) {
//        // 1.上传到Oss,并返回远程url
//        String url = aliOssService.upload(file);
//        // 2.扫描识别
//        // 定义扫描出的结果
//        ApiResult<List<String>> apiResultList;
//        DeriveExcel result = new DeriveExcel();
//        // 若结果集没有更新就是失败了
//        ApiResult<String> apiResult = ApiResultHandler.buildApiResult(-1,"失败",url);
//        try {
//            // 扫描出来所有的结果集
//            apiResultList = ocrService.scan(file);
//            // 获取结果集
//            List<String> data = apiResultList.getData();
//
//            // 结果集第一行为学号
//            String username = autoInfoService.autoUsername(data.get(0));
//            result.setUsername(username);
//
//            result.setAffiliation(autoInfoService.autoClass(username));
//            result.setName(autoInfoService.autoName(username));
//            result.setPaper(new URL(url));
//            // 结果集第二行为选择题答案，第三行为判断题答案
//            String scanAnswer = autoInfoService.autoMultiQ(data.get(1)) + autoInfoService.autoJudgeQ(data.get(2));
//            result.setScanAnswer(scanAnswer);
//            System.out.println("\n\n扫描出来的答案是："+scanAnswer+"\n\n");
//            result.setObjScore(autoInfoService.autoObjScore(scanAnswer,answer));
//
//            apiResult = easyPoiService.write(result);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return apiResult;
//    }

    @Override
    public String exam(MultipartFile file,String answer,AnswerNumValue answerNumValue) {
        // 1.上传到Oss,并返回远程url
        String url = aliOssService.upload(file);
        // 2.扫描识别
        // 定义扫描出的结果
        ApiResult<List<String>> apiResultList;
        DeriveExcel result = new DeriveExcel();
        // 若结果集没有更新就是失败了
        String apiResult = "失败";
        try {
            // 扫描出来所有的结果集
            apiResultList = ocrService.scanFileByMultipartFile(file);
            // 获取结果集
            List<String> data = apiResultList.getData();

            List<String> rightData = autoInfoService.autoData2RightData(data, answer);

            // 结果集第一行为学号
            String username = autoInfoService.autoUsername(rightData.get(0));
            result.setUsername(username);
            result.setAffiliation(autoInfoService.autoClass(username));
            result.setName(autoInfoService.autoName(username));
            result.setPaper(url);
            result.setPaperImg(new URL(url));
            // 结果集第二行为选择题答案，第三行为判断题答案
            // todo
            String scanAnswer = rightData.get(1) + rightData.get(2);
//            Double scanAnswer =Integer.valueOf(rightData.get(1))  + Integer.valueOf(rightData.get(2));

            result.setScanAnswer(scanAnswer);
//            result.setScanAnswer(Double.parseDouble(scanAnswer));
            System.out.println("\n\n扫描出来的答案是："+scanAnswer+"\n\n");
            result.setObjScore(Double.parseDouble(autoInfoService.autoObjScore(scanAnswer,answer,answerNumValue)));
//            result.setObjScore(Double.parseDouble(autoInfoService.autoObjScore(scanAnswer,answer)));
            result.setSubScore(Double.parseDouble("0"));
            // 更新至数据库中
            deriveExcelService.insert(result);

            apiResult = easyPoiService.write(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return apiResult;
    }

    @Autowired
    private AdminService adminService;

    @Override
    public String examByUrlAndId(List<DeriveExcel> deriveExcelList, String answer, AnswerNumValue answerNumValue) {
        // 2.扫描识别
        // 定义扫描出的结果
        List<String> data = new ArrayList<>();

        // 若结果集没有更新就是失败了
        String apiResult = "失败";
        for (DeriveExcel deriveExcel : deriveExcelList) {
            DeriveExcel result = new DeriveExcel();
            try {
                data = ocrService.scanByUrl(deriveExcel.getPaper());
                List<String> rightData = autoInfoService.autoData2RightData(data, answer);
                result.setId(deriveExcel.getId());

//                // 根据上传人的身份确定 信息是否可靠
//                if("学生".equals(deriveExcel.getCommitRole())){
//                    // 因为学生是只能手动上传所以可信 提交的username就是正确的username
//                    result.setUsername(deriveExcel.getCommitUsername());
//                    Admin admin = adminService.selectByUsername(deriveExcel.getCommitUsername());
//                    result.setAffiliation(admin.getAffiliation());
//                    result.setName(admin.getName());
//                }else{
//                    // 剩余情况为统一导入，按照识别的为主
//                    // 结果集第一行为学号
//                    String username = autoInfoService.autoUsername(rightData.get(0));
//                    result.setUsername(username);
//                    result.setAffiliation(autoInfoService.autoClass(username));
//                    result.setName(autoInfoService.autoName(username));
//                }

            if(!"学生".equals(deriveExcel.getCommitRole())){
                // 剩余情况为统一导入，按照识别的为主
                // 结果集第一行为学号
                String username = autoInfoService.autoUsername(rightData.get(0));
                result.setUsername(username);
                result.setAffiliation(autoInfoService.autoClass(username));
                result.setName(autoInfoService.autoName(username));
            }

                result.setPaper(deriveExcel.getPaper());
                result.setPaperImg(new URL(deriveExcel.getPaper()));
                result.setExamId(deriveExcel.getExamId());
                // 结果集第二行为选择题答案，第三行为判断题答案
                String scanAnswer = rightData.get(1) + rightData.get(2);
                result.setScanAnswer(scanAnswer);
                System.out.println("\n\n扫描出来的答案是："+scanAnswer+"\n\n");
                result.setObjScore(Double.parseDouble(autoInfoService.autoObjScore(scanAnswer,answer,answerNumValue)));
                result.setSubScore(Double.parseDouble("0"));
                result.setScore(result.getObjScore());
                // 更新至数据库中
                deriveExcelService.updateOrInsert(result);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return apiResult;
    }


}
